package com.pangda.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pangda.common.domain.PageResult;
import com.pangda.common.domain.ResponseResult;
import com.pangda.common.utils.UserContext;
import com.pangda.pojo.SysMenu;
import com.pangda.pojo.dto.SysMenuDto;
import com.pangda.pojo.vo.MenuTreeVo;
import com.pangda.pojo.vo.NodeVo;
import com.pangda.pojo.vo.SysMenuVo;
import com.pangda.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author xjy
 * @since 2024-06-20
 */
@RestController
@RequestMapping("/system/menu")
@Api(tags = "菜单权限")
public class SysMenuController {
    @Autowired
    private ISysMenuService sysMenuService;

    @PostMapping
    @ApiOperation("新增菜单")
    public ResponseResult<Void> add(@RequestBody SysMenuDto sysMenuDto) {
        return sysMenuService.add(sysMenuDto);
    }

    @GetMapping("/list")
    @ApiOperation("获取菜单列表")
    public ResponseResult<List<SysMenuVo>> list(SysMenuDto sysMenuDto) {
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(ObjectUtil.isNotEmpty(sysMenuDto.getMenuName()), SysMenu::getMenuName, sysMenuDto.getMenuName())
                .eq(ObjectUtil.isNotEmpty(sysMenuDto.getStatus()), SysMenu::getStatus, sysMenuDto.getStatus());
        List<SysMenu> list = sysMenuService.list(queryWrapper);
        return ResponseResult.success(BeanUtil.copyToList(list, SysMenuVo.class));
    }

    @GetMapping("/{menuId}")
    @ApiOperation("获取菜单详情")
    public ResponseResult<SysMenuVo> get(@PathVariable Long menuId) {
        SysMenu sysMenu = sysMenuService.getById(menuId);
        return ResponseResult.success(BeanUtil.copyProperties(sysMenu, SysMenuVo.class));
    }

    @PutMapping
    @ApiOperation("修改菜单")
    public ResponseResult<Void> update(@RequestBody SysMenuDto sysMenuDto) {
        if (ObjectUtil.isEmpty(sysMenuDto.getMenuId())) {
            return ResponseResult.error();
        }
        sysMenuDto.setUpdateBy(UserContext.getUser());
        sysMenuDto.setUpdateTime(String.valueOf(LocalDateTime.now()));
        sysMenuService.updateById(BeanUtil.toBean(sysMenuDto, SysMenu.class));
        return ResponseResult.success();
    }

    @DeleteMapping("/{menuId}")
    @ApiOperation("删除菜单")
    public ResponseResult delete(@PathVariable Long menuId) {
        return sysMenuService.deleteMenu(menuId);
    }

    @GetMapping("/treeselect")
    @ApiOperation("菜单下拉树列表")
    public ResponseResult<NodeVo> treeSelect() {
        return sysMenuService.treeSelect();
    }

    @GetMapping("/roleMenuTreeselect/{roleId}")
    @ApiOperation("角色菜单树列表")
    public MenuTreeVo roleMenuTreeSelect(@PathVariable("roleId") Long roleId) {
        return sysMenuService.roleMenuTreeSelect(roleId);
    }
}
